package cn.dnui_zzh807.dnuifood_16110100807.model;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.widget.Toast;

import java.util.List;

import cn.dnui_zzh807.dnuifood_16110100807.R;
import cn.dnui_zzh807.dnuifood_16110100807.bean.UserCollectBean;
import cn.dnui_zzh807.dnuifood_16110100807.controller.BaseActivity;
import cn.dnui_zzh807.dnuifood_16110100807.listener.OnRetrofitListener;
import cn.dnui_zzh807.dnuifood_16110100807.service.CollectService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class CollectModel extends AppCompatActivity {
    private Context context;
    private BaseActivity baseActivity;
    public CollectModel(){


    }

    public void isCollect(String user_id, String shop_food_id, String flag, final Menu menu){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<UserCollectBean> call = collectService.isCollected(user_id,shop_food_id,flag);
        Callback<UserCollectBean> callback = new Callback<UserCollectBean>() {
            @Override
            public void onResponse(Call<UserCollectBean> call, Response<UserCollectBean> response) {
                // listener.onSuccess(response.body(),5);
                if (response.body().getCollected().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");
                    // Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();
                }else {
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                    //Toast.makeText(context,"取消收藏成功",Toast.LENGTH_SHORT).show();

                }
            }

            @Override
            public void onFailure(Call<UserCollectBean> call, Throwable t) {
                Toast.makeText(CollectModel.this,"网络连接失败",Toast.LENGTH_SHORT).show();
                //listener.onFail();
            }
        };
        call.enqueue(callback);
    }

    public void userCollectShop(String user_id, String shop_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<UserCollectBean> call = collectService.userCollectShop(user_id,shop_id);
        Callback<UserCollectBean> callback = new Callback<UserCollectBean>() {
            @Override
            public void onResponse(Call<UserCollectBean> call, Response<UserCollectBean> response) {
                //listener.onSuccess(response.body(),6);
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");
                    // baseActivity.showToast("收藏成功");

                }else {
                    Toast.makeText(CollectModel.this, "收藏店铺失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UserCollectBean> call, Throwable t) {
                // listener.onFail();
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);

    }

    public void unCollectShop(String user_id, final String shop_id, final List<UserCollectBean> mDataList){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<UserCollectBean> call = collectService.userCollectShop(user_id,shop_id);
        Callback<UserCollectBean> callback = new Callback<UserCollectBean>() {
            @Override
            public void onResponse(Call<UserCollectBean> call, Response<UserCollectBean> response) {
                if (response.body().getSuccess().equals("1")){


                }else {
                    // Toast.makeText(context, "取消收藏失败", Toast.LENGTH_SHORT).show();
                }

            }


            @Override
            public void onFailure(Call<UserCollectBean> call, Throwable t) {
                baseActivity.showToast("网络连接失败");
            }
        };
        call.enqueue(callback);

    }
    public void unCollectShop_01(String user_id, String shop_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<UserCollectBean> call = collectService.userCollectShop(user_id,shop_id);
        Callback<UserCollectBean> callback = new Callback<UserCollectBean>() {
            @Override
            public void onResponse(Call<UserCollectBean> call, Response<UserCollectBean> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                    // baseActivity.showToast("取消收藏成功");

                }else {
                    //baseActivity.showToast("取消收藏失败");
                }

            }


            @Override
            public void onFailure(Call<UserCollectBean> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };

        call.enqueue(callback);
    }

    public void unCollectFood(String user_id, final String food_id, final List<UserCollectBean> mDataList){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<UserCollectBean> call = collectService.userCollectFood(user_id,food_id);
        Callback<UserCollectBean> callback = new Callback<UserCollectBean>() {
            @Override
            public void onResponse(Call<UserCollectBean> call, Response<UserCollectBean> response) {
                if (response.body().getSuccess().equals("1")){

                }else {
                }

            }


            @Override
            public void onFailure(Call<UserCollectBean> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);

    }
    public void unCollectFood_01(String user_id, final String food_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<UserCollectBean> call = collectService.userCollectFood(user_id,food_id);
        Callback<UserCollectBean> callback = new Callback<UserCollectBean>() {
            @Override
            public void onResponse(Call<UserCollectBean> call, Response<UserCollectBean> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("收藏");
                }else {
                    // Toast.makeText(context, "取消收藏店铺失败", Toast.LENGTH_SHORT).show();
                }

            }
            @Override
            public void onFailure(Call<UserCollectBean> call, Throwable t) {
                Toast.makeText(context, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        };
        call.enqueue(callback);

    }

    public void userCollectFood(String user_id, String food_id, final Menu menu){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<UserCollectBean> call = collectService.userCollectFood(user_id,food_id);
        Callback<UserCollectBean> callback = new Callback<UserCollectBean>() {
            @Override
            public void onResponse(Call<UserCollectBean> call, Response<UserCollectBean> response) {
                if (response.body().getSuccess().equals("1")){
                    menu.findItem(R.id.item_collect).setTitle("已收藏");
                    // baseActivity.showToast("收藏成功");

                }else {
                    Toast.makeText(CollectModel.this, "收藏店铺失败", Toast.LENGTH_SHORT).show();
                }
            }
            @Override
            public void onFailure(Call<UserCollectBean> call, Throwable t) {

            }
        };

        call.enqueue(callback);
    }


    public void getAllUserCollection(String user_id,String flag,final OnRetrofitListener listener){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://172.24.10.175:8080/foodService/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CollectService collectService = retrofit.create(CollectService.class);
        Call<List<UserCollectBean>> call = collectService.getAllUserCollection(user_id,flag);
        Callback<List<UserCollectBean>> callback = new Callback<List<UserCollectBean>>() {
            @Override
            public void onResponse(Call<List<UserCollectBean>> call, Response<List<UserCollectBean>> response) {

                listener.onSuccess(response.body(),8);

                // Toast.makeText(context,"收藏成功",Toast.LENGTH_SHORT).show();

                //Toast.makeText(context,"取消收藏成功",Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onFailure(Call<List<UserCollectBean>> call, Throwable t) {
                Toast.makeText(context,"网络连接失败",Toast.LENGTH_SHORT).show();
                listener.onFail();
            }
        };
        call.enqueue(callback);
    }

}
